//
//  LGDietRecordCell.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/3/12.
//

#import "LGDietRecordCell.h"
#import "LGCheckPhotoTool.h"
#define photoW  viewPix(50)

@interface LGDietRecordCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *titleBtn;
/**   */
@property (nonatomic , strong)UILabel *takeLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)NSDictionary *iconDic;
/**   */
@property (nonatomic , strong)NSMutableArray *imageArray;

@end

@implementation LGDietRecordCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.iconDic = @{@"zero":@"饮食记录-早餐",@"one":@"饮食记录-午餐",@"two":@"饮食记录-晚餐",@"three":@"饮食记录-加餐"};
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *iconName = self.iconDic[dataDic[@"tag"]];
    [self.titleBtn setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    [self.titleBtn setTitle:LGNSString(dataDic[@"title"]) forState:UIControlStateNormal];
    //take
    NSString *take = LGNSString(dataDic[@"take_str"]);
    self.takeLabel.text = take;
    CGFloat takeTop = (take.length>0)?viewPix(10):0;
    CGFloat takeWidth = (take.length>0)?[take textWidthWithFont:LGFontWeight(12, UIFontWeightMedium) maxHeight:MAXFLOAT]+viewPix(25):0;
    CGFloat takeHeight = (take.length>0)?viewPix(20):0;
    [self.takeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleBtn.mas_bottom).offset(takeTop);
        make.width.equalTo(@(takeWidth));
        make.height.equalTo(@(takeHeight));
    }];
    
    //content
    NSString *content = LGNSString(dataDic[@"take_desc"]);
    self.contentLabel.text = content;
    [self.contentLabel lineSpacing:3];
    CGFloat contentTop = (content.length>0)?viewPix(8):0;
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.takeLabel.mas_bottom).offset(contentTop);
    }];
    
    
    //image
    for (UIImageView *imageView in self.imageArray) {
        [imageView removeFromSuperview];
    }
    [self.imageArray removeAllObjects];
    NSArray *photoArray = dataDic[@"imgs"];
    CGFloat totalHeight = 0;
    CGFloat maxWidth = Screen_W-viewPix(56);
    CGFloat left = 0;
    CGFloat top = 0;
    for (NSInteger i=0; i<photoArray.count; i++) {
        top = (left+viewPix(50))>maxWidth?top+viewPix(60):top;
        left = (left+viewPix(50))>maxWidth?0:left;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, top, viewPix(50), viewPix(50))];
        imageView.tag = 1000+i;
        [self.photoView addSubview:imageView];
        [self.imageArray addObject:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i]]];
        totalHeight = top+viewPix(50);
        left += viewPix(60);
    }
    CGFloat imageTop = (totalHeight>0)?viewPix(8):0;
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(imageTop);
        make.height.equalTo(@(totalHeight));
    }];
}

-(void)checkPhotoAction:(UITapGestureRecognizer *)sender{
    NSInteger index = 0;
    CGPoint point = [sender locationInView:self.photoView];
    for (UIImageView *imageView in self.imageArray) {
        if(CGRectContainsPoint(imageView.frame, point)){
            index = imageView.tag-1000;
            break;
        }
    }
    LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    photoView.picArray = self.dataDic[@"imgs"];
    photoView.title = @"";
    [[ObjectTool mainWindow] addSubview:photoView];
    [photoView showViewAnimation:CGPointMake(Screen_W/2.0, Screen_H/2.0) index:index];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleBtn];
    [self.baseView addSubview:self.takeLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.photoView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.takeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleBtn.mas_bottom).offset(viewPix(11));
        make.left.equalTo(self.titleBtn);
        make.width.height.equalTo(@(0));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.takeLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.titleBtn);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.titleBtn);
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        make.height.equalTo(@(0));
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B0B2B2" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(UIButton *)titleBtn{
    if(!_titleBtn){
        _titleBtn = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#282828" imageName:@""];
        _titleBtn.frame = CGRectMake(viewPix(12), viewPix(10), Screen_W-viewPix(56), viewPix(20));
        _titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    }
    return _titleBtn;
}

-(UILabel *)takeLabel{
    if(!_takeLabel){
        _takeLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(12, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _takeLabel.backgroundColor = [UIColor colorWithString:kThemeColor];
        _takeLabel.cornerRidus = viewPix(10);
    }
    return _takeLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#606266" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc] init];
        [_photoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkPhotoAction:)]];
    }
    return _photoView;
}

-(NSMutableArray *)imageArray{
    if(!_imageArray){
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

@end
